package com.jic.order.controller.rest;

import com.jic.common.base.vo.RestResult;
import com.jic.order.rest.request.*;
import com.jic.order.rest.response.CartSettleAddressResponse;
import com.jic.order.rest.response.CartSettleResponse;
import com.jic.order.api.SettleFeign;
import com.jic.order.service.SettleService;
import com.jic.order.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author: gaolanyu
 * @date: 2020-01-23
 * @remark:
 */
@RestController
@RequestMapping("/trade-settle")
@Api(tags = {"结算H5API"})
@Slf4j
public class SettleController implements SettleFeign {
    @Autowired
    SettleService settleService;
    @Autowired
    ShoppingCartService shoppingCartService;

    @Override
    @ApiOperation("不加购物车直接结算")
    public RestResult<CartSettleResponse> directSettle(DirectSettleRequest directSettleRequest) {
        String userId=shoppingCartService.getUserIdByToken(directSettleRequest.getToken());
        if(!StringUtils.isEmpty(userId)){
            directSettleRequest.setUserId(Long.parseLong(userId));
        }
        log.info("SettleController==>directSettle==>,userId:{},req:{}",directSettleRequest.getUserId(),directSettleRequest);
        return settleService.directSettle(directSettleRequest);
    }

    @Override
    @ApiOperation("砍价商品结算")
    public RestResult<CartSettleResponse> grouponSettle(DirectSettleRequest directSettleRequest) {
        String userId=shoppingCartService.getUserIdByToken(directSettleRequest.getToken());
        if(!StringUtils.isEmpty(userId)){
            directSettleRequest.setUserId(Long.parseLong(userId));
        }
        log.info("SettleController==>directSettle==>,userId:{},req:{}",directSettleRequest.getUserId(),directSettleRequest);
        return settleService.grouponSettle(directSettleRequest);
    }

    @ApiOperation("结算接口")
    public RestResult<CartSettleResponse> settle(CartSettleRequest cartSettleRequest) {
        String userId=shoppingCartService.getUserIdByToken(cartSettleRequest.getToken());
        if(!StringUtils.isEmpty(userId)){
            cartSettleRequest.setUserId(Long.parseLong(userId));
        }
        return settleService.settle(cartSettleRequest, false);
    }

    @ApiOperation("选择收货地址")
    public RestResult<CartSettleAddressResponse> address(CartSettleAddressRequest cartSettleAddressRequest) {
        String userId=shoppingCartService.getUserIdByToken(cartSettleAddressRequest.getToken());
        if(!StringUtils.isEmpty(userId)){
            cartSettleAddressRequest.setUserId(Long.parseLong(userId));
        }
        RestResult<Boolean> directFlagResponse=settleService.getDirectFlagByShoppingCartId(cartSettleAddressRequest.getShoppingCartId(),cartSettleAddressRequest.getUserId());
        if(!RestResult.checkIsSuccess(directFlagResponse)){
            return RestResult.error(directFlagResponse.getCode(),directFlagResponse.getMsg());
        }

        log.info("SettleController==>address==>,userId:{},req:{},directFlag:{}",cartSettleAddressRequest.getUserId(),cartSettleAddressRequest,directFlagResponse.getData());
        RestResult<CartSettleAddressResponse> responseRestResult=settleService.address(cartSettleAddressRequest,directFlagResponse.getData());
        log.info("SettleController==>address==>,userId:{},resp:{},directFlag:{},data:{}",cartSettleAddressRequest.getUserId(),responseRestResult,directFlagResponse.getData(),responseRestResult.getData());
        return responseRestResult;
    }

    @ApiOperation("选择要使用的优惠券")
    public RestResult<BigDecimal> coupon(CartSettleCouponRequest cartSettleCouponRequest) {
        return null;
    }

    @ApiOperation("提交订单")
    public RestResult<String> initOrder(CartOrderRequest cartOrderRequest) {
        String userId=shoppingCartService.getUserIdByToken(cartOrderRequest.getToken());
        if(!StringUtils.isEmpty(userId)){
            cartOrderRequest.setUserId(Long.parseLong(userId));
        }
        RestResult<Boolean> directFlagResponse=settleService.getDirectFlagByShoppingCartId(cartOrderRequest.getShoppingCartId(),cartOrderRequest.getUserId());
        if(!RestResult.checkIsSuccess(directFlagResponse)){
            return RestResult.error(directFlagResponse.getCode(),directFlagResponse.getMsg());
        }
        return settleService.initOrder(cartOrderRequest,directFlagResponse.getData());
    }
}
